package thomp.robots;

import battlecode.common.Robot;
import battlecode.common.RobotController;

public abstract class RobotBase {
    
    public static RobotController myRC;
    
    //-- Constants --//
    public static double MIN_FLUX = 25.0d;
    
    public RobotBase (RobotController rc) {
        myRC = rc;
    }
    
    // Attempt to attack an enemy
    public boolean attack() {

        try {
            
            if (myRC.isAttackActive())
                return false;
            
            // Determine if an enemy is within range
            // IF enemy visible
            //      shoot it
            Robot[] bots = myRC.senseNearbyGameObjects(Robot.class);
            for (Robot bot : bots) {
                if ((bot.getTeam() == myRC.getTeam().opponent())
                        && (myRC.canAttackSquare(myRC.senseLocationOf(bot)))) {

                    myRC.attackSquare(
                            myRC.senseLocationOf(bot),
                            bot.getRobotLevel());
                    return true;
                }
            }
        } catch (Exception e) {
            System.out.println("caught exception:");
            e.printStackTrace();
        }
        return false;
    }
    
    // Move around looking for a target
    public void wander() {
                  
        try {
            // Wait until we are allowed to move
            if (myRC.isMovementActive()) {
                return;
            }
            
            // If we can't move, signal for help
            if (myRC.getFlux() < myRC.getType().moveCost) {
                System.out.print("Out of gas\n");
                return;
            }                
            
            // Move forward if we can, otherwise turn right
            if (myRC.canMove(myRC.getDirection())) {
                myRC.moveForward();
            } else {
                myRC.setDirection(myRC.getDirection().rotateRight());
            }
        } catch (Exception e) {
            System.out.println("caught exception:");
            e.printStackTrace();
        }
    }    
}
